package cn.echo.modules.sys.interceptor;

import cn.echo.modules.sys.utils.SessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Loginterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object object) throws Exception {
       String username=request.getParameter("username");
        String password=request.getParameter("password");
        SqlSession session= (SqlSession) SessionFactory.getSession();
       request.getSession().setAttribute("user_session",username);

        System.out.println(username);
        // 已经登录，继续执行
        if (request.getSession().getAttribute("user_session") != null) {

            return true;
        } else {//未登录，跳转到登录页面
            response.sendRedirect(request.getContextPath() + "/shibai.jsp");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {
    }


    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object object, Exception e) throws Exception {
    }
}

